/*
package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

*/
/**
 * 用户关注或者取消关注的业务
 *//*

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    private ArticleFeign articleFeign;
    //取消或者关注操作
    @Override
    public ResponseResult follow(UserRelationDto dto) {

        //1.参数检查
        if(dto.getOperation() == null || dto.getOperation()<0 || dto.getOperation() > 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getAuthorId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"作者信息不能为空");
        }

        //2.获取作者
        ApAuthor apAuthor = articleFeign.findById(dto.getAuthorId());
        //TODO 判断还未完
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"需要登录之后才能操作");
        }
        Integer followId=null;
        if(apAuthor!=null){
            followId=apAuthor.getUserId();
        }

        //3.判断是否是关注还是取消关注操作
        if(dto.getOperation()==0){//关注
            //保存用户粉丝信息和关注信息
           return followByUserId(apUser,followId,apAuthor,dto.getArticleId());
        }else  if(dto.getOperation()==1){//取消关注
            //删除用户粉丝信息和关注信息
           return followCancelByUserId(apUser,followId,apAuthor);
        }

        return null;
    }


    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    //保存关注业务
    private ResponseResult followByUserId(ApUser apUser,Integer followId,ApAuthor apAuthor,Long articeId) {

        //先根据用户id和关注作者id去查询一下apUserFollow，如果存在则表示已关注，否则添加关注
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().
                eq(ApUserFollow::getUserId, apUser.getId()).
                eq(ApUserFollow::getFollowId, followId));

        if(apUserFollow==null){
            //保存用户粉丝信息和关注信息

            //保存关注
            apUserFollow=new ApUserFollow();
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setFollowId(followId);
            apUserFollow.setFollowName(apAuthor.getName());
            apUserFollow.setIsNotice(true);
            apUserFollow.setLevel((short)1);
            apUserFollow.setUserId(apUser.getId());
            apUserFollowMapper.insert(apUserFollow);
            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().
                    eq(ApUserFan::getUserId, apAuthor.getUserId()).
                    eq(ApUserFan::getFansId, apUser.getId()));
            if(apUserFan==null){
                //保存粉丝
                apUserFan=new ApUserFan();
                apUserFan.setUserId(apAuthor.getUserId());
                apUserFan.setFansId(apUser.getId().longValue());
                apUserFan.setFansName(apUser.getName());
                apUserFan.setLevel((short)0);
                apUserFan.setIsDisplay(true);
                apUserFan.setIsShieldLetter(false);
                apUserFan.setIsShieldComment(false);
                apUserFan.setCreatedTime(new Date());
                apUserFanMapper.insert(apUserFan);
            }
            //TODO 异步记录关注行为。。。。。
            FollowBehaviorDto dto=new FollowBehaviorDto();
            dto.setArticleId(articeId);
            dto.setFollowId(followId);
            dto.setUserId(apUser.getId());

            kafkaTemplate.send(BehaviorConstants.FOLLOW_BEHAVIOR_TOPIC, JSON.toJSONString(dto));


            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已关注");
        }
    }


    //取消关注业务
    private ResponseResult followCancelByUserId(ApUser apUser,Integer followId,ApAuthor apAuthor) {
        //删除用户粉丝信息和关注信息
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().
                eq(ApUserFollow::getUserId, apUser.getId()).
                eq(ApUserFollow::getFollowId, followId));
        if(apUserFollow!=null){
            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().
                    eq(ApUserFollow::getUserId, apUser.getId()).
                    eq(ApUserFollow::getFollowId, followId));

            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().
                    eq(ApUserFan::getUserId, apAuthor.getUserId()).
                    eq(ApUserFan::getFansId, apUser.getId()));
            if(apUserFan!=null){
                apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().
                        eq(ApUserFan::getUserId, apAuthor.getUserId()).
                        eq(ApUserFan::getFansId, apUser.getId()));
            }
            return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"没有关注的数据");
        }
    }
}
*/
